FUNCTION YBC_SWS_DP_SEND.
*"----------------------------------------------------------------------
*"*"本地接口：
*"  IMPORTING
*"     REFERENCE(BDCID) TYPE  /OPT/CP_BDCID
*"     REFERENCE(ONLINE) DEFAULT SPACE
*"     REFERENCE(INDEX_HEADER) TYPE  /OPT/VIM_1HEAD
*"     REFERENCE(SIMULATE) TYPE  XFELD OPTIONAL
*"  EXPORTING
*"     REFERENCE(SUCCESSFUL)
*"  TABLES
*"      INDEX_ITEM STRUCTURE  /OPT/VIM_1ITEM OPTIONAL
*"      RETURN STRUCTURE  BAPIRET2 OPTIONAL
*"  EXCEPTIONS
*"      INVALID_DATA
*"----------------------------------------------------------------------

  DATA:
    LV_SEND_FLAG TYPE C,
    LS_DSPMSG    TYPE /OPT/STRUCT_MESSAGE.

  DATA:
    LV_AMOUNT    TYPE P DECIMALS 2,
    LV_BUSID     TYPE YBC_SWS_REQUEST-BUSINESSID,
    LS_HEAD_DATA TYPE YBCS_SWS_DATA_H,
    LS_ITEM_DATA TYPE YBCS_SWS_DATA_I,
    LT_HEAD_DATA TYPE STANDARD TABLE OF YBCS_SWS_DATA_H,
    LT_ITEM_DATA TYPE STANDARD TABLE OF YBCS_SWS_DATA_I,
    LS_APPROVER  TYPE YBCS_SWS_APPROVER,
    LT_APPROVER  TYPE STANDARD TABLE OF YBCS_SWS_APPROVER,
    LS_ATTACH    TYPE YBCS_SWS_ATTACH01,
    LT_ATTACH    TYPE YBCT_SWS_ATTACH01,
    LS_MSG       TYPE BAPIRET2,
    LV_MESSAGE   TYPE STRING.

  DATA:
    LV_DATA_X   TYPE XSTRING,
    LV_LEN      TYPE I,
    LV_MINETYPE TYPE CHAR200,
    LT_DATA_OPT TYPE STANDARD TABLE OF SDOKCNTBIN WITH HEADER LINE.

  IF SIMULATE IS NOT INITIAL.
    RETURN.
  ENDIF.

  LV_BUSID = INDEX_HEADER-DOCID.

  SELECT *
    INTO TABLE @DATA(LT_SWS_REQUEST)
    FROM YBC_SWS_REQUEST
    WHERE BUSINESSID = @LV_BUSID.

  SORT LT_SWS_REQUEST BY CDATE DESCENDING CTIME DESCENDING.
  READ TABLE LT_SWS_REQUEST INTO DATA(LS_SWS_REQUEST) INDEX 1.

  IF LS_SWS_REQUEST-STATUS = 'S'."已审批通过
    RETURN.
  ENDIF.

  IF LS_SWS_REQUEST-STATUS = '0' OR LS_SWS_REQUEST-STATUS = '1'. "已提交、处理中
    RAISE INVALID_DATA.
    RETURN.
  ENDIF.
  DATA:
    LT_MESSAGE      TYPE TABLE OF /OPT/STRUCT_MESSAGE WITH HEADER LINE,
    LS_INDEX_HEADER TYPE /OPT/VIM_1HEAD.

  LS_INDEX_HEADER = INDEX_HEADER.

  "模拟过账无报错
  "无PO模拟过账
  CALL FUNCTION '/OPT/VIM_DETERMINE_PROC_450'
    EXPORTING
      IV_NO_UPDATE  = 'X'
      IV_PROC_TYPE  = '271'
* IMPORTING
*     TAB_DSPMSG    =
    TABLES
      MESSAGES      = LT_MESSAGE[]
    CHANGING
      INDEX_DATA    = LS_INDEX_HEADER
*     INDEX_DATA2   =
      CT_INDEX_ITEM = INDEX_ITEM[]
    EXCEPTIONS
      CHECK_FAILED  = 1
      OTHERS        = 2.
  IF SY-SUBRC <> 0.
    RAISE INVALID_DATA.
    RETURN.
  ENDIF.

  "自定义检查无报错


  "开始发送数据
  CALL FUNCTION 'YBC_SWS_CANCEL_REQUEST'
    EXPORTING
*     IV_REQUESTID      =
      IV_APPID          = 'INV_NPO'
      IV_BUSINESSID     = LV_BUSID
      IV_NO_EXIST_CHECK = 'X'
*     IV_NO_STATUS_CHECK       =
*   IMPORTING
*     ES_MSG            =
    .

  "根据结构定义，自动赋值抬头行项目值
  CALL FUNCTION 'YBC_SWS_PREPARE_DATA'
    EXPORTING
      IV_HEADER    = INDEX_HEADER
    IMPORTING
      ES_MSG       = LS_MSG
    TABLES
      IT_ITEM      = INDEX_ITEM[]
      ET_HEAD_DATA = LT_HEAD_DATA
      ET_ITEM_DATA = LT_ITEM_DATA.

  IF LS_MSG-TYPE = 'E'.
    RAISE INVALID_DATA.
    RETURN.
  ENDIF.

  SELECT *
   FROM TOA01
   INTO TABLE @DATA(LT_TOA01)
  WHERE OBJECT_ID = @INDEX_HEADER-DOCID
    AND SAP_OBJECT = '/OPT/V1001'.

  LOOP AT LT_TOA01 INTO DATA(LS_TOA01).
    CLEAR LS_ATTACH.

    CALL FUNCTION 'SCMS_HTTP_GET'
      EXPORTING
*       MANDT                 = SY-MANDT
        CREP_ID               = LS_TOA01-ARCHIV_ID
        DOC_ID                = LS_TOA01-ARC_DOC_ID
      IMPORTING
        LENGTH                = LV_LEN
        MIMETYPE              = LV_MINETYPE
      TABLES
        DATA                  = LT_DATA_OPT
      EXCEPTIONS
        BAD_REQUEST           = 1
        UNAUTHORIZED          = 2
        NOT_FOUND             = 3
        CONFLICT              = 4
        INTERNAL_SERVER_ERROR = 5
        ERROR_HTTP            = 6
        ERROR_URL             = 7
        ERROR_SIGNATURE       = 8
        OTHERS                = 9.

    IF LT_DATA_OPT[] IS NOT INITIAL.

      CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
        EXPORTING
          INPUT_LENGTH = LV_LEN
        IMPORTING
          BUFFER       = LS_ATTACH-CONTENT
        TABLES
          BINARY_TAB   = LT_DATA_OPT
        EXCEPTIONS
          FAILED       = 1
          OTHERS       = 2.

    ENDIF.

    LS_ATTACH-FILENAME = LS_TOA01-ARC_DOC_ID.
    LS_ATTACH-FILESIZE = LV_LEN.
    LS_ATTACH-FILETYPE = 'pdf'.
    LS_ATTACH-MIMETYPE = LV_MINETYPE.
    APPEND LS_ATTACH TO LT_ATTACH.

  ENDLOOP.

  CLEAR LS_MSG.
  CALL FUNCTION 'YBC_SWS_SEND_FIRST'
    EXPORTING
      IV_APPID      = 'INV_NPO'
      IV_TEMPLID    = 'INV_NPO'
      IV_BUSINESSID = LV_BUSID
*     IV_UNAME      = SY-UNAME
    IMPORTING
      ES_MSG        = LS_MSG
    TABLES
      IT_HEAD_DATA  = LT_HEAD_DATA
      IT_ITEM_DATA  = LT_ITEM_DATA
*     IT_APPROVER   =
      IT_ATTACH     = LT_ATTACH.

  IF LS_MSG-TYPE = 'E'.
    RAISE INVALID_DATA.
    RETURN.
  ELSE.

  ENDIF.

ENDFUNCTION.
